package com.krlin.word.word.controller;

import com.krlin.word.word.init.RedisCache;
import com.krlin.word.word.pojo.WordEntity;
import com.krlin.word.word.service.WordMongoService;
import com.krlin.word.word.service.WordRedisService;
import com.krlin.word.word.utils.CodeMsg;
import com.krlin.word.word.utils.Result;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author krlin
 */
@RestController
@RequestMapping("/word")
public class WordController {

    @Resource
    private WordRedisService wordRedisService;

    @RequestMapping("/getOne/{range}")
    public Result getOneWordByRand(@PathVariable String range) {
        if (RedisCache.ALL_LETTER.equals(range)) {
            WordEntity oneWord = wordRedisService.getOneWordFromAllWords();
            if (oneWord == null) {
                return Result.error(CodeMsg.DATA_ERROR);
            } else {
                return Result.ok().put("word", oneWord);
            }

        } else {
            WordEntity oneWord = wordRedisService.getOneWordFromCertainType(range);
            if (oneWord == null) {
                return Result.error(CodeMsg.DATA_ERROR);
            } else {
                return Result.ok().put("word", oneWord);
            }

        }
    }
}
